package com.product.web;

import com.product.exception.AppException;
import com.product.exception.SelectNotException;
import com.product.pojo.Product;
import com.product.service.ProductService;
import com.product.service.impl.ProductServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

//ProductServlet是一个司令官。他负责调度其他组件来完成任务。
@WebServlet({"/findAll","/edit","/Update","/delete","/add"})
@MultipartConfig
public class ProductServlet extends HttpServlet {
    ProductService productService = new ProductServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取session
        HttpSession session = request.getSession(false);
        //session在jsp中会默认创建
        if (session != null && session.getAttribute("username") != null) {

            request.setCharacterEncoding("utf-8");
            //获取servlet路径
            String servletPath = request.getServletPath();
            if ("/findAll".equals(servletPath)) {
                doFindAll(request,response);
            } else if ("/edit".equals(servletPath)) {
                doEdit(request,response);
            } else if ("/Update".equals(servletPath)) {
                doUpdate(request,response);
            } else if ("/delete".equals(servletPath)) {
                dodelete(request,response);
            } else if ("/add".equals(servletPath)) {
                doAdd(request,response);

            }
        }else {
            response.sendRedirect("" + request.getContextPath() + "/login.jsp");
        }
    }

    private void doAdd(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        int id= Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String price = request.getParameter("price");
        String type = request.getParameter("type");
        //获取二进制文件数据
        Part part = request.getPart("photo");
        //获取二进制文件名
        String fileName = part.getSubmittedFileName();
        //把文件数据写入服务器磁盘上
        part.write("D:\\uplocfile" + fileName);
        String photo = fileName;
        Product product = new Product();
        product.setId(id);
        product.setName(name);
        product.setPrice(price);
        product.setType(type);
        product.setPhoto(photo);
        try {
            int count = productService.add(product);
            if (count == 1) {
                //添加成功
                response.sendRedirect("" + request.getContextPath() + "/findAll");
            } else {
                response.sendRedirect("" + request.getContextPath() + "/error.jsp");

            }
        } catch (SelectNotException | AppException e) {
            response.sendRedirect("" + request.getContextPath() + "/error.jsp");
        }
    }

    private void dodelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        //获取前端发送的数据
        String id = request.getParameter("id");
        int count = 0;
        try {
            count = productService.delete(Integer.parseInt(id));
            if (count == 1) {
                //说明删除成功
                response.sendRedirect("" + request.getContextPath() + "/findAll");
            } else {
                response.sendRedirect("" + request.getContextPath() + "/error.jsp");
            }
        }catch (SelectNotException | AppException e) {
            response.sendRedirect("" + request.getContextPath() + "/error.jsp");
        }
    }

    private void doUpdate(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String price = request.getParameter("price");
        String type = request.getParameter("type");
        String photo = request.getParameter("photo");
        Product product = new Product();
        product.setId(Integer.parseInt(id));
        product.setName(name);
        product.setPrice(price);
        product.setType(type);
        product.setPhoto(photo);
        int count = 0;
        try {
            count = productService.upDate(product);
            if (count == 1) {
                //说明修改成功了
                response.sendRedirect("" + request.getContextPath() + "/findAll");
            } else {
                response.sendRedirect("" + request.getContextPath() + "/error.jsp");
            }
        } catch (SelectNotException | AppException e) {
            response.sendRedirect("" + request.getContextPath() + "/error.jsp");
        }
    }

    private void doEdit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        //获取传过来的name
        String productName = request.getParameter("name");
        try {
            Product product = productService.findOne(productName);
            request.setAttribute("product", product);
            request.getRequestDispatcher("/edit.jsp").forward(request, response);

        }catch (SelectNotException | AppException e) {
            response.sendRedirect("" + request.getContextPath() + "/error.jsp");
        }

    }

    private void doFindAll(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{

        List<Product> list = null;
        try {
            list = productService.findAll();
            // 执行到这里了，说明成功了
            //集合list存到域中
            request.setAttribute("productList", list);
            request.getRequestDispatcher("/list.jsp").forward(request, response);
        } catch (SelectNotException | AppException e) {
            response.sendRedirect("" + request.getContextPath() + "/error.jsp");
        }


    }
}
